﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace BYUStock
{
    public partial class UpdateStocks : Form
    {
        string MyEncoding = "UTF-8";
        WebClient Client = new WebClient();

        public UpdateStocks()
        {
            InitializeComponent();
            UpdateStockList();
        }
        public class Stock
        {
            public string dm;
            public string mc;
            public string jys;
        }
        public class KDJ
        {
            public DateTime t;
            public float k;
            public float d;
            public float j;
        }

        void UpdateStockList()
        {
            byte[] bytes = Client.DownloadData(Global.StockNameList_MR);
            string content = Encoding.GetEncoding(MyEncoding).GetString(bytes);
            Console.WriteLine(content);
            var stocks = JsonConvert.DeserializeObject<List<Stock>>(content);
            int updated = 0;
            int inserted = 0;
            int kdjInserted = 0;
            using (var conn = new SQLiteConnection(DbHelper.ConnStr))
            {
                conn.Open();
                using (var cmd = new SQLiteCommand())
                {
                    cmd.Connection = conn;

                    foreach (var stock in stocks)
                    {
                        var sql = $"select Name from StockBaseInfo where Code = '{stock.dm}';";
                        cmd.CommandText = sql;
                        var name = cmd.ExecuteScalar();
                        if (name != null)
                        {
                            if (name.ToString() != stock.mc)
                            {
                                var sql2 = $"update StockBaseInfo set Name = '{stock.mc}',Exchange = '{stock.jys}' where Code = '{stock.dm}';";
                                cmd.CommandText = sql;
                                var result = cmd.ExecuteNonQuery();
                                if (result == 1)
                                {
                                    updated++;
                                }
                            }
                        }
                        else
                        {
                            var sql3 = $"insert into StockBaseInfo(Code,Name,Exchange,Kind) values('{stock.dm}','{stock.mc}','{stock.jys}',3);";
                            cmd.CommandText = sql;
                            var result = cmd.ExecuteNonQuery();
                            if (result == 1)
                            {
                                inserted++;
                            }
                        }
                        var uri = Global.StockLastKDJ_MR(stock.dm);
                        byte[] kdjBytes = Client.DownloadData(uri);
                        string kdjContent = Encoding.GetEncoding(MyEncoding).GetString(kdjBytes);
                        var kdj = JsonConvert.DeserializeObject<KDJ>(kdjContent);
                        {
                            if (kdj != null)
                            {
                                var sqlKdjExsit = $"select j from stock_kdjs where code = '{stock.dm}' and time = '{kdj.t}'";
                                cmd.CommandText = sqlKdjExsit;
                                var kdjExsit = cmd.ExecuteScalar();
                                if (kdjExsit == null)
                                {
                                    var sqlInsertKdj = $"insert into stock_kdjs values('{stock.dm}','{kdj.t}','{kdj.k}','{kdj.d}','{kdj.j}')";
                                    cmd.CommandText = sqlInsertKdj;
                                    var result = cmd.ExecuteNonQuery();
                                    if (result == 1)
                                    {
                                        kdjInserted++;
                                    }
                                }
                            }
                            //KDJ的表需要重填;
                        }
                    }
                }
            }
            MessageBox.Show($"更新：{updated}支，新增：{inserted}支\r\n" +
                            $"更新KDJ：{kdjInserted}条");
        }
    }
}
